package android.slc.extras.user.repository.remote;

import android.slc.adapter.PageModel;
import android.slc.appbase.api.slc.callback.po.SlcEntity;
import android.slc.appbase.api.slc.config.ApiConfig;
import android.slc.appbase.repository.remote.PageTransformUtils;
import android.slc.extras.user.entity.LoginResult;
import android.slc.extras.user.entity.UserInfo;
import android.slc.extras.user.repository.local.UserDaoServiceRepository;
import android.slc.or.SlcNu;
import android.slc.or.SlcParam;
import android.slc.rx.SlcRxJavaUtils;

import java.util.List;

import io.reactivex.rxjava3.core.Observable;

/**
 * @author slc
 * @date 2020-06-19 14:44
 */
public class UserServiceRepository {

    /**
     * 用户登录
     *
     * @param account
     * @param password
     * @return
     */
    public static Observable<SlcEntity<UserInfo>> login(String account, String password) {
        return SlcNu.getInstance().create(UserService.class)
                .login(SlcParam.newBuilder().put("username", account).put("password", password).build())
                .map(slcEntity -> {
                    LoginResult loginResult = slcEntity.getData();
                    ApiConfig.getInstance().setToken(loginResult.getToken());
                    SlcEntity<UserInfo> userSlcEntity = new SlcEntity<>();
                    userSlcEntity.setCode(slcEntity.getCode());
                    userSlcEntity.setMsg(slcEntity.getMsg());
                    userSlcEntity.setData(loginResult.getUser());
                    return userSlcEntity;
                }).compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }

    /**
     * 根据用户ID获取用户信息
     *
     * @param userId
     * @return
     */
    public static Observable<SlcEntity<UserInfo>> getUserById(Long userId) {
        return SlcNu.getInstance().create(UserService.class)
                .getUserById(userId)
                .map(UserDaoServiceRepository.saveUserMap())
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    public static Observable<SlcEntity<List<UserInfo>>> list(long deptId) {
        SlcParam.Builder builder = SlcParam.newBuilder();
        builder.put("deptId", deptId);
        if (deptId > 0) {
        }
        return SlcNu.getInstance().create(UserService.class)
                .list(builder.build())
                .map(UserDaoServiceRepository.saveUserListMap())
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    public static Observable<SlcEntity<PageModel<UserInfo>>> page(long deptId, int pageNum) {
        SlcParam.Builder builder = SlcParam.newBuilder();
        builder.put("deptId", deptId);
        if (deptId > 0) {
        }
        return SlcNu.getInstance().create(UserService.class)
                .list(builder
                        .put("pageNum", pageNum)
                        .put("pageSize", ApiConfig.getInstance().getPageSize())
                        .build())
                .map(UserDaoServiceRepository.saveUserListMap())
                .map(PageTransformUtils.list2PageModelMap())
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }

    /**
     * 获取部门列表
     *
     * @return
     */
    public static Observable<SlcEntity<PageModel<UserInfo>>> listByIds(List<String> ids) {
        return SlcNu.getInstance().create(UserService.class)
                .listByIds(ids)
                .map(PageTransformUtils.list2PageModelMap())
                .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
    }
}